/*
 * @Date: 2024-11-22 11:10:01
 * @Author: LiSong
 * @LastEditors: LiSong
 * @LastEditTime: 2025-02-14 18:04:25
 * @Description: 
 */
import { loadAsyncComponent } from '@hp/utils/component.js'

/**
 * 初始化组件的属性配置选项
 * 
 * @param {Array} configArray 
 * @returns {Array}
 */
const initConfigAttrs = (configArray) => {
  // 添加固定属性配置
  configArray[0].children = [
    { type: 'input', field: 'id', label: 'ID',
      compProps: { readonly: true, disabled: true  }
    },
    { type: 'input', field: 'label', label: '标题', compProps: {} },
    ...configArray[0].children,
  ]
  
  /**
   * 设置文字提示
   * 
   * @param {Object} item 配置信息
   */
  const setPlaceholder = (item) => {
    if (['input'].includes(item.type)) {
      item.compProps['placeholder'] = '请输入'
    } else if (['select', 'input-icon'].includes(item.type)) {
      item.compProps['placeholder'] = '请选择'
    }
  }

  /**
   * 递归配置
   * 
   * @param {Array} list 配置
   * @returns 
   */
  const recursionConfig = (list) => {
    return list.map(item => {
      setPlaceholder(item)
      if (item.children && item.children.length > 0) {
        item.children = item.children.map(child => {
          setPlaceholder(child)
          if (child.children && child.children.length > 0) {
            child.children = recursionConfig(child.children)
          }
          return child
        })
      } 
      return item
    })
  }

  configArray = recursionConfig(configArray)
  return configArray
}

class PluginManager {
  constructor () {
    // 请求方式
    this.request = {}
    // 面板的Ref
    this.panelRefs = {}
    // 面板组件
    this.panelComps = {}
    // 面板分组
    this.panelGroup = new Map()
    // 配置属性的组件
    this.components = {}
    // 属性配置的组件
    this.attrEditors = {}
    // 面板的配置
    this.panelConfig = {}
  }

  /**
   * 注册面板组件
   * 
   * @param {Object} panelInfo 面板信息
   * @param {Object} groupInfo 分组信息
   */
  registerPanels (panelInfo, groupInfo) {
    if (typeof panelInfo.component === 'function') {
      panelInfo.component = loadAsyncComponent(panelInfo.component)
    }
    this.panelComps[panelInfo.type] = panelInfo.component
    this.components[panelInfo.type] = panelInfo.component
    this.panelConfig[panelInfo.type] = initConfigAttrs(panelInfo.config)

    // 插入面板分组
    if (!this.panelGroup.has(groupInfo.type)) {
      let groupItem = {
        ...groupInfo,
        children: [ panelInfo ]
      }
      this.panelGroup.set(groupInfo.type, groupItem)
    } else {
      this.panelGroup.get(groupInfo.type).children.push(panelInfo)
    }
  }

  /**
   * 注册面板Ref
   * 
   * @param {String} id 
   * @param {Object} refObj 
   */
  registerPanelsRef (id, refObj) {
    this.panelRefs[id] = refObj
  }

  /**
   * 注册属性配置的编辑点击组件
   * 
   * @param {Object} item 
   */
  registerAttrEditor (item) {
    let comp = item.component
    if (typeof comp === 'function') {
      comp = loadAsyncComponent(comp)
    }
    this.components[item.type] = comp
    this.attrEditors[item.type] = comp
  }
  /**
   * 获取面板的Ref
   * 
   * @param {String} id 面板Id
   * @returns 
   */
  getPanelRef (id) {
    return this.panelRefs[id]
  }

  getPanelComp (type) {
    return this.panelComps[type]
  }

  getPanelGroupValues () {
    let values = []
    this.panelGroup.forEach((value) => {
      values.push(value)
    })
    values.sort((a, b) => a.order - b.order)
    return values
  }

  /**
   * 清空面板的Ref
   */
  clearPanelRefs () {
    this.panelRefs = {}
  }
  /**
   * 移除的Ref
   * 
   * @param {String} id 面板ID
   */
  removePanelRef (id) {
    delete this.panelRefs[id]
  }
}

export const pluginManager = new PluginManager();
